package com.yunchang.springboot.scheduled.mwsfinance;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.mapper.finance.FinanceCaravanDebitCreditRelationMapper;
import com.yunchang.springboot.mapper.mwsfinance.*;
import com.yunchang.springboot.mapper.mwsutils.FOverseasWarehouseQuantityMapper;
import com.yunchang.springboot.models.DO.finance.FinanceCaravanDebitCreditRelation;
import com.yunchang.springboot.models.mwsfinance.*;
import com.yunchang.springboot.models.mwsutils.FOverseasWarehouseQuantity;
import com.yunchang.springboot.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 出入库明细
 */
@Component
@Configuration
@Service
@Slf4j
@EnableScheduling
public class FInventoryDetailsScheduled extends BaseService {

    static final List<String> EU_COUNTRY_LIST = Arrays.asList("DE", "FR", "IT", "UK", "ES");

    /**
     * 库存数据
     */
    @Autowired
    FInventoryDailyScheduled fInventoryDailyScheduled;

    /**
     * payment销售退款（包含下推数据在途物资）
     */
    @Autowired
    FPaymentRefundScheduled fPaymentRefundScheduled;

    /**
     * 亚马逊退货
     */
    @Autowired
    FFbaCustomerReturnScheduled fFbaCustomerReturnScheduled;

    /**
     * 赔偿
     */
    @Autowired
    FReimbursementsScheduled fReimbursementsScheduled;

    /**
     * 盘库数据
     */
    @Autowired
    FAdjustmentsScheduled fAdjustmentsScheduled;

    /**
     * 已完成订单
     */
    @Autowired
    FFbaShipmentSalesScheduled fFbaShipmentSalesScheduled;

    /**
     * 移除货件
     */
    @Autowired
    FRemovalScheduled fRemovalScheduled;

    /**
     * 期末盘库
     */
    @Autowired
    FAdjustmentInventoryMonthlyScheduled fAdjustmentInventoryMonthlyScheduled;


    /**
     * 已接收库存财务数据
     */
    private FReceiveMapper fReceiveMapper;

    @Autowired
    public void setFReceiveMapper(FReceiveMapper fReceiveMapper) {
        this.fReceiveMapper = fReceiveMapper;
    }

    /**
     * 在途物资财务数据
     */
    private FGoodsInTransitMapper fGoodsInTransitMapper;

    @Autowired
    public void setFGoodsInTransitMapper(FGoodsInTransitMapper fGoodsInTransitMapper) {
        this.fGoodsInTransitMapper = fGoodsInTransitMapper;
    }

    /**
     * 亚马逊退货财务数据
     */
    private FFbaCustomerReturnMapper fFbaCustomerReturnMapper;

    @Autowired
    public void setFFbaCustomerReturnMapper(FFbaCustomerReturnMapper fFbaCustomerReturnMapper) {
        this.fFbaCustomerReturnMapper = fFbaCustomerReturnMapper;
    }

    /**
     * 盘库财务数据
     */
    private FAdjustmentsMapper fAdjustmentsMapper;

    @Autowired
    public void setFAdjustmentsMapper(FAdjustmentsMapper fAdjustmentsMapper) {
        this.fAdjustmentsMapper = fAdjustmentsMapper;
    }

    /**
     * 已完成订单财务数据
     */
    private FFbaShipmentSalesMapper fFbaShipmentSalesMapper;

    @Autowired
    public void setFFbaShipmentSalesMapper(FFbaShipmentSalesMapper fFbaShipmentSalesMapper) {
        this.fFbaShipmentSalesMapper = fFbaShipmentSalesMapper;
    }

    /**
     * 移除货件财务数据
     */
    private FRemovalMapper fRemovalMapper;

    @Autowired
    public void setFRemovalMapper(FRemovalMapper fRemovalMapper) {
        this.fRemovalMapper = fRemovalMapper;
    }

    /**
     * 赔偿财务数据下推数据
     */
    private FReimbursementQuantityMapper fReimbursementQuantityMapper;

    @Autowired
    public void setFReimbursementQuantityMapper(FReimbursementQuantityMapper fReimbursementQuantityMapper) {
        this.fReimbursementQuantityMapper = fReimbursementQuantityMapper;
    }

    /**
     * 期末盘库
     */
    private FAdjustmentInventoryMonthlyMapper fAdjustmentInventoryMonthlyMapper;

    @Autowired
    public void setFAdjustmentInventoryMonthlyMapper(FAdjustmentInventoryMonthlyMapper fAdjustmentInventoryMonthlyMapper) {
        this.fAdjustmentInventoryMonthlyMapper = fAdjustmentInventoryMonthlyMapper;
    }

    /**
     * 各账单记录对应借贷关系
     */
    private FinanceCaravanDebitCreditRelationMapper financeCaravanDebitCreditRelationMapper;

    @Autowired
    public void setFinanceCaravanDebitCreditRelationMapper(FinanceCaravanDebitCreditRelationMapper financeCaravanDebitCreditRelationMapper) {
        this.financeCaravanDebitCreditRelationMapper = financeCaravanDebitCreditRelationMapper;
    }

    /**
     * 海外仓数据
     */
    private FOverseasWarehouseQuantityMapper fOverseasWarehouseQuantityMapper;

    @Autowired
    public void setFOverseasWarehouseQuantityMapper(FOverseasWarehouseQuantityMapper fOverseasWarehouseQuantityMapper) {
        this.fOverseasWarehouseQuantityMapper = fOverseasWarehouseQuantityMapper;
    }

    /**
     * 已接收库存财务数据生成出入库明细
     */
    public void getFInventoryDetailsByFReceive(String date) throws CloneNotSupportedException {
        List<FReceive> fReceiveList = fReceiveMapper.selectByIsInventoryDetailsAndDate(date, 1);
        if (null != fReceiveList && !fReceiveList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FReceive fReceive : fReceiveList) {
                Integer isNeed = isDebitOrCredit(fReceive.getDebitSubjectId(), fReceive.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fReceive.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fReceive.getDate());
                fInventoryDetails.setSellerId(fReceive.getSellerId());
                fInventoryDetails.setCountry(fReceive.getCountry());
                fInventoryDetails.setSku(fReceive.getSku());
                fInventoryDetails.setFnsku(fReceive.getFnsku());
                fInventoryDetails.setQuantity(fReceive.getQuantity());
                fInventoryDetails.setCostUnit(fReceive.getCostUnit());
                fInventoryDetails.setAmount(fReceive.getAmount());
                fInventoryDetails.setReason("receive");
                fInventoryDetails.setCurrency(fReceive.getCurrency());
                fInventoryDetails.setStockSku(fReceive.getStockSku());
                fInventoryDetails.setRelatedOrderId(fReceive.getShipmentId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fReceive.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fReceive.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fReceive.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fReceive.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fReceiveMapper.updateIsInventoryDetailsByFReceiveList(fReceiveList);
                log.info("已接收库存对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，已经接收库存财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，已经接收库存财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的已经接收库存财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 在途物资财务数据生成出入库明细
     */
    public void getFInventoryDetailsByFGoodsInTransit(String date) throws CloneNotSupportedException {
        List<FGoodsInTransit> fGoodsInTransitList = fGoodsInTransitMapper.selectByPostedDateAndIsInventoryDetails(date, 1);
        if (null != fGoodsInTransitList && !fGoodsInTransitList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FGoodsInTransit fGoodsInTransit : fGoodsInTransitList) {
                Integer isNeed = isDebitOrCredit(fGoodsInTransit.getDebitSubjectId(), fGoodsInTransit.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fGoodsInTransit.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fGoodsInTransit.getPostedDate());
                fInventoryDetails.setSellerId(fGoodsInTransit.getSellerId());
                fInventoryDetails.setCountry(fGoodsInTransit.getCountry());
                fInventoryDetails.setSku(fGoodsInTransit.getSku());
                fInventoryDetails.setQuantity(fGoodsInTransit.getQuantity());
                fInventoryDetails.setCostUnit(fGoodsInTransit.getCostUnit());
                fInventoryDetails.setAmount(fGoodsInTransit.getAmount());
                fInventoryDetails.setCurrency(fGoodsInTransit.getCurrency());
                fInventoryDetails.setReason("refund");
                fInventoryDetails.setRelatedOrderId(fGoodsInTransit.getOrderId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fGoodsInTransit.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fGoodsInTransit.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fGoodsInTransit.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fGoodsInTransit.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fGoodsInTransitMapper.updateIsInventoryDetailsByFGoodsInTransitList(fGoodsInTransitList);
                log.info("在途物资对应的出入库明细已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，在途物资财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，在途物资财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的在途物资财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }


    /**
     * 亚马逊退货财务数据生成出入库明细
     */
    public void getFInventoryDetailsByFFbaCustomerReturn(String date) throws CloneNotSupportedException {
        List<FFbaCustomerReturn> fFbaCustomerReturnList = fFbaCustomerReturnMapper.selectByReturnDateAndIsInventoryDetails(date, 1);
        if (null != fFbaCustomerReturnList && !fFbaCustomerReturnList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FFbaCustomerReturn fFbaCustomerReturn : fFbaCustomerReturnList) {
                Integer isNeed = isDebitOrCredit(fFbaCustomerReturn.getDebitSubjectId(), fFbaCustomerReturn.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fFbaCustomerReturn.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fFbaCustomerReturn.getReturnDate());
                fInventoryDetails.setSellerId(fFbaCustomerReturn.getSellerId());
                fInventoryDetails.setCountry(fFbaCustomerReturn.getCountryShip());
                fInventoryDetails.setSku(fFbaCustomerReturn.getSku());
                fInventoryDetails.setFnsku(fFbaCustomerReturn.getFnsku());
                fInventoryDetails.setAsin(fFbaCustomerReturn.getAsin());
                fInventoryDetails.setQuantity(fFbaCustomerReturn.getQuantity());
                fInventoryDetails.setCostUnit(fFbaCustomerReturn.getCostUnit());
                fInventoryDetails.setAmount(fFbaCustomerReturn.getAmount());
                fInventoryDetails.setCurrency(fFbaCustomerReturn.getCurrency());
                fInventoryDetails.setReason("customer_return");
                fInventoryDetails.setRelatedOrderId(fFbaCustomerReturn.getOrderId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fFbaCustomerReturn.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fFbaCustomerReturn.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fFbaCustomerReturn.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fFbaCustomerReturn.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fFbaCustomerReturnMapper.updateIsInventoryDetailsByFFbaCustomerReturnList(fFbaCustomerReturnList);
                log.info("亚马逊退货对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，亚马逊退货财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，亚马逊退货财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的亚马逊退货财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 盘库财务数据生成出入库明细
     *
     * @param reasonList
     */
    public void getFInventoryDetailsByFAdjustments(List<String> reasonList, String date) throws CloneNotSupportedException {
        List<FAdjustments> fAdjustmentsList = fAdjustmentsMapper.selectByDateAndIsInventoryDetailsAndReasonList(date, 1, reasonList);
        if (null != fAdjustmentsList && !fAdjustmentsList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FAdjustments fAdjustments : fAdjustmentsList) {
                Integer isNeed = isDebitOrCredit(fAdjustments.getDebitSubjectId(), fAdjustments.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fAdjustments.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fAdjustments.getDate());
                fInventoryDetails.setSellerId(fAdjustments.getSellerId());
                fInventoryDetails.setCountry(fAdjustments.getCountry());
                fInventoryDetails.setSku(fAdjustments.getSku());
                fInventoryDetails.setFnsku(fAdjustments.getFnsku());
                fInventoryDetails.setQuantity(fAdjustments.getQuantity());
                fInventoryDetails.setCostUnit(fAdjustments.getCostUnit());
                fInventoryDetails.setAmount(fAdjustments.getAmount());
                fInventoryDetails.setCurrency(fAdjustments.getCurrency());
                fInventoryDetails.setReason("adjustments");
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fAdjustments.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fAdjustments.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fAdjustments.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fAdjustments.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fAdjustmentsMapper.updateIsInventoryDetailsByFAdjustmentsList(fAdjustmentsList);
                log.info("盘库财务数据对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，盘库财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，盘库财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的盘库财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据赔偿的下推数据生成出入库明细数据
     */
    public void getFInventoryDetailsByFReimbursementQuantity(String date) throws CloneNotSupportedException {
        List<FReimbursementQuantity> fReimbursementQuantityList = fReimbursementQuantityMapper.selectByIsInventoryDetailsAndDate(date, 1);
        if (null != fReimbursementQuantityList && !fReimbursementQuantityList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FReimbursementQuantity fReimbursementQuantity : fReimbursementQuantityList) {
                Integer isNeed = isDebitOrCredit(fReimbursementQuantity.getDebitSubjectId(), fReimbursementQuantity.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fReimbursementQuantity.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fReimbursementQuantity.getDate());
                fInventoryDetails.setSellerId(fReimbursementQuantity.getSellerId());
                fInventoryDetails.setCountry(fReimbursementQuantity.getCountry());
                fInventoryDetails.setSku(fReimbursementQuantity.getSku());
                fInventoryDetails.setFnsku(fReimbursementQuantity.getFnsku());
                fInventoryDetails.setQuantity(fReimbursementQuantity.getQuantity());
                fInventoryDetails.setCostUnit(fReimbursementQuantity.getCostUnit());
                fInventoryDetails.setAmount(fReimbursementQuantity.getAmount());
                fInventoryDetails.setCurrency(fReimbursementQuantity.getCurrency());
                fInventoryDetails.setReason("reimbursement");
                fInventoryDetails.setRelatedOrderId(fReimbursementQuantity.getReimbursementId());
                fInventoryDetails.setRelatedOriginalOrderId(fReimbursementQuantity.getAmazonOrderId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fReimbursementQuantity.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fReimbursementQuantity.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fReimbursementQuantity.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fReimbursementQuantity.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fReimbursementQuantityMapper.updateIsInventoryDetailsByFReimbursementQuantityList(fReimbursementQuantityList);
                log.info("赔偿下推数据对应的出入库明细数据已生成");
            } catch (Exception e) {
                log.error("插入数据时出现错误，赔偿下推数据财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，赔偿下推数据财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的赔偿下推数据财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据已完成订单生成出入库明细数据
     */
    public void getFInventoryDetailsByFFbaShipmentSales(String date) throws CloneNotSupportedException {
        List<FFbaShipmentSales> fFbaShipmentSalesList = fFbaShipmentSalesMapper.selectByIsInventoryDetailsAndDate(date, 1);
        if (null != fFbaShipmentSalesList && !fFbaShipmentSalesList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FFbaShipmentSales fFbaShipmentSales : fFbaShipmentSalesList) {
                Integer isNeed = isDebitOrCredit(fFbaShipmentSales.getDebitSubjectId(), fFbaShipmentSales.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fFbaShipmentSales.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fFbaShipmentSales.getDate());
                fInventoryDetails.setSellerId(fFbaShipmentSales.getSellerId());
                fInventoryDetails.setCountry(fFbaShipmentSales.getCountryShip());
                fInventoryDetails.setSku(fFbaShipmentSales.getSku());
                fInventoryDetails.setFnsku(fFbaShipmentSales.getFnsku());
                fInventoryDetails.setAsin(fFbaShipmentSales.getAsin());
                fInventoryDetails.setQuantity(fFbaShipmentSales.getQuantity());
                fInventoryDetails.setCostUnit(fFbaShipmentSales.getCostUnit());
                fInventoryDetails.setAmount(fFbaShipmentSales.getAmount());
                fInventoryDetails.setCurrency(fFbaShipmentSales.getCurrency());
                fInventoryDetails.setReason("sales");
                fInventoryDetails.setRelatedOrderId(fFbaShipmentSales.getAmazonOrderId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fFbaShipmentSales.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fFbaShipmentSales.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fFbaShipmentSales.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fFbaShipmentSales.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fFbaShipmentSalesMapper.updateIsInventoryDetailsByFFbaShipmentSalesList(fFbaShipmentSalesList);
                log.info("已完成订单对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，已完成订单财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，已完成订单财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的已完成订单财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }


    /**
     * 根据移除货件财务数据生成出入库明细数据
     */
    public void getFInventoryDetailsByFRemoval(String date) throws CloneNotSupportedException {
        List<FRemoval> fRemovalList = fRemovalMapper.selectByIsInventoryDetailsAndShipmentDate(date, 1);
        if (null != fRemovalList && !fRemovalList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FRemoval fRemoval : fRemovalList) {
                Integer isNeed = isDebitOrCredit(fRemoval.getDebitSubjectId(), fRemoval.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fRemoval.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fRemoval.getShipmentDate());
                fInventoryDetails.setSellerId(fRemoval.getSellerId());
                fInventoryDetails.setCountry(fRemoval.getCountry());
                fInventoryDetails.setSku(fRemoval.getSku());
                fInventoryDetails.setStockSku(fRemoval.getStockSku());
                fInventoryDetails.setFnsku(fRemoval.getFnsku());
                fInventoryDetails.setQuantity(fRemoval.getQuantity());
                fInventoryDetails.setCostUnit(fRemoval.getCostUnit());
                fInventoryDetails.setAmount(fRemoval.getAmount());
                fInventoryDetails.setCurrency(fRemoval.getCurrency());
                fInventoryDetails.setReason("removal");
                fInventoryDetails.setRelatedOrderId(fRemoval.getOrderId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fRemoval.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fRemoval.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fRemoval.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fRemoval.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fRemovalMapper.updateIsInventoryDetailsByFRemovalList(fRemovalList);
                log.info("移除货件对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，移除货件财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，移除货件财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的移除货件财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据期末盘库数据生成出入库明细数据
     */
    public void getFInventoryDetailsByFAdjustmentInventoryMonthly(String date) throws CloneNotSupportedException {
        List<FAdjustmentInventoryMonthly> fAdjustmentInventoryMonthlyList = fAdjustmentInventoryMonthlyMapper.selectByIsInventoryDetailsAndDate(date, 1);
        if (null != fAdjustmentInventoryMonthlyList && !fAdjustmentInventoryMonthlyList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FAdjustmentInventoryMonthly fAdjustmentInventoryMonthly : fAdjustmentInventoryMonthlyList) {
                Integer isNeed = isDebitOrCredit(fAdjustmentInventoryMonthly.getDebitSubjectId(), fAdjustmentInventoryMonthly.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fAdjustmentInventoryMonthly.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setDate(fAdjustmentInventoryMonthly.getDate());
                fInventoryDetails.setSellerId(fAdjustmentInventoryMonthly.getSellerId());
                fInventoryDetails.setCountry(fAdjustmentInventoryMonthly.getCountry());
                fInventoryDetails.setSku(fAdjustmentInventoryMonthly.getSku());
                fInventoryDetails.setFnsku(fAdjustmentInventoryMonthly.getFnsku());
                fInventoryDetails.setQuantity(fAdjustmentInventoryMonthly.getQuantity());
                fInventoryDetails.setCostUnit(fAdjustmentInventoryMonthly.getCostUnit());
                fInventoryDetails.setAmount(fAdjustmentInventoryMonthly.getAmount());
                fInventoryDetails.setCurrency(fAdjustmentInventoryMonthly.getCurrency());
                fInventoryDetails.setReason("adjustments_monthly");
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fAdjustmentInventoryMonthly.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fAdjustmentInventoryMonthly.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fAdjustmentInventoryMonthly.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fAdjustmentInventoryMonthly.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fAdjustmentInventoryMonthlyMapper.updateIsInventoryDetailsByFAdjustmentInventoryMonthlyList(fAdjustmentInventoryMonthlyList);
                log.info("期末盘库对应的出入库明细数据已生成" + date);
            } catch (Exception e) {
                log.error("插入数据时出现错误，期末盘库财务数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，期末盘库财务数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的期末盘库财务数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据各账单记录对应借贷关系生成出入库明细数据
     */
    public void getFInventoryDetailsByFinanceCaravanDebitCreditRelation(String date) {
        List<String> subjectList = Arrays.asList("140203", "140204", "140205", "140210");
        List<FinanceCaravanDebitCreditRelation> financeCaravanDebitCreditRelationList = financeCaravanDebitCreditRelationMapper.selectByDataTimeAndIsInventoryDetails(date, 1, subjectList);
        if (null != financeCaravanDebitCreditRelationList && !financeCaravanDebitCreditRelationList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FinanceCaravanDebitCreditRelation financeCaravanDebitCreditRelation : financeCaravanDebitCreditRelationList) {
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setDate(financeCaravanDebitCreditRelation.getDataTime());
                fInventoryDetails.setSellerId(financeCaravanDebitCreditRelation.getSellerId());
                String country = financeCaravanDebitCreditRelation.getArea();
                if (EU_COUNTRY_LIST.contains(country)) {
                    country = "EU";
                }
                fInventoryDetails.setCountry(country);
                fInventoryDetails.setStockSku(financeCaravanDebitCreditRelation.getSku());
                Integer loanType = financeCaravanDebitCreditRelation.getLoanType();
                fInventoryDetails.setType(getType(loanType));
                fInventoryDetails.setQuantity(financeCaravanDebitCreditRelation.getSkuNum());
                fInventoryDetails.setAmount(financeCaravanDebitCreditRelation.getAmount().doubleValue());
                fInventoryDetails.setCurrency("RMB");
                fInventoryDetails.setReason("shipment");
                fInventoryDetails.setRelatedOrderId(financeCaravanDebitCreditRelation.getRecordNo());
                fInventoryDetails.setWarehouse(financeCaravanDebitCreditRelation.getSubjectId().toString());
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                financeCaravanDebitCreditRelation.setIsInventoryDetails(2);
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                financeCaravanDebitCreditRelationMapper.updateIsInventoryDetailsByFinanceCaravanDebitCreditRelationList(financeCaravanDebitCreditRelationList);
                log.info("马帮数据对应的出入库明细数据已生成");
            } catch (Exception e) {
                log.error("插入数据时出现错误，各账单记录对应借贷关系数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，各账单记录对应借贷关系数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的各账单记录对应借贷关系数据，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据海外仓数据生成出入库数据
     */
    public void getFInventoryDetailsByFOverseasWarehouseQuantity(String date) throws CloneNotSupportedException {
        List<FOverseasWarehouseQuantity> fOverseasWarehouseQuantityList = fOverseasWarehouseQuantityMapper.selectByIsInventoryDetailsAndDate(date, 1);
        if (null != fOverseasWarehouseQuantityList && !fOverseasWarehouseQuantityList.isEmpty()) {
            List<Object> fInventoryDetailsInsertList = new ArrayList<>();
            for (FOverseasWarehouseQuantity fOverseasWarehouseQuantity : fOverseasWarehouseQuantityList) {
                Integer isNeed = isDebitOrCredit(fOverseasWarehouseQuantity.getDebitSubjectId(), fOverseasWarehouseQuantity.getCreditSubjectId());
                FInventoryDetails fInventoryDetails = new FInventoryDetails();
                fInventoryDetails.setIsInventoryDaily(1);
                fInventoryDetails.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fInventoryDetails.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fOverseasWarehouseQuantity.setIsInventoryDetails(2);
                if (0 == isNeed) {
                    continue;
                }
                fInventoryDetails.setSellerId(fOverseasWarehouseQuantity.getSellerId());
                fInventoryDetails.setDate(fOverseasWarehouseQuantity.getDate());
                fInventoryDetails.setCountry(fOverseasWarehouseQuantity.getCountry());
                fInventoryDetails.setSku(fOverseasWarehouseQuantity.getSku());
                fInventoryDetails.setStockSku(fOverseasWarehouseQuantity.getStockSku());
                fInventoryDetails.setQuantity(fOverseasWarehouseQuantity.getQuantity());
                fInventoryDetails.setCostUnit(fOverseasWarehouseQuantity.getCostUnit());
                fInventoryDetails.setAmount(fOverseasWarehouseQuantity.getAmount());
                fInventoryDetails.setCurrency(fOverseasWarehouseQuantity.getCurrency());
                fInventoryDetails.setReason("overseas_warehouse");
                fInventoryDetails.setRelatedOrderId(fOverseasWarehouseQuantity.getShipmentId());
                if (1 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fOverseasWarehouseQuantity.getCreditSubjectId());
                }
                if (2 == isNeed) {
                    fInventoryDetails.setType(2);
                    fInventoryDetails.setWarehouse(fOverseasWarehouseQuantity.getDebitSubjectId());
                }
                if (3 == isNeed) {
                    fInventoryDetails.setType(1);
                    fInventoryDetails.setWarehouse(fOverseasWarehouseQuantity.getCreditSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails);
                    FInventoryDetails fInventoryDetails1 = (FInventoryDetails) fInventoryDetails.clone();
                    fInventoryDetails1.setType(2);
                    fInventoryDetails1.setWarehouse(fOverseasWarehouseQuantity.getDebitSubjectId());
                    fInventoryDetailsInsertList.add(fInventoryDetails1);
                    continue;
                }
                fInventoryDetailsInsertList.add(fInventoryDetails);
            }
            try {
                baseInsertList(fInventoryDetailsInsertList);
                fOverseasWarehouseQuantityMapper.updateIsInventoryDetailsByFOverseasWarehouseQuantityList(fOverseasWarehouseQuantityList);
                log.info("海外仓数据对应的出入库明细数据已生成");
            } catch (Exception e) {
                log.error("插入数据时出现错误，海外仓数据生成出入库明细数据" + e);
                throw new RuntimeException("插入数据时出现错误，海外仓数据生成出入库明细数据");
            }
        } else {
            log.info("未查询到对应的海外仓数据，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }


    /**
     * 根据借方科目和贷方科目判断是否需要将该条数据放到出入库明细中
     *
     * @param debitSubjectId
     * @param creditSubjectId
     * @return 0:两者都不包含 1：贷方  2：借方  3：两者都包含
     */
    public Integer isDebitOrCredit(String debitSubjectId, String creditSubjectId) {
        if (!StringUtils.isEmpty(debitSubjectId) && !StringUtils.isEmpty(creditSubjectId)) {
            if ((debitSubjectId.startsWith("1406") || debitSubjectId.startsWith("1402")) && (creditSubjectId.startsWith("1406") || creditSubjectId.startsWith("1402"))) {
                return 3;
            } else if (debitSubjectId.startsWith("1406") || debitSubjectId.startsWith("1402")) {
                return 2;
            } else if (creditSubjectId.startsWith("1406") || creditSubjectId.startsWith("1402")) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 根据
     *
     * @param loanType
     * @return
     */
    public Integer getType(Integer loanType) {
        if (null != loanType) {
            if (loanType == 1) {
                return 2;
            }
            if (loanType == 2) {
                return 1;
            }
        }
        log.info("各账单记录对应借贷关系生成出入库明细数据是获取type时出错，loanType不等于1或2");
        return null;
    }

}
